<?php
namespace App\Http\Controllers;
use App\AdminUser;
use App\Task;
use App\TaskType;
use App\Customer;
use App\Product;
use Psy\Util\Json;
use Illuminate\Support\Facades\DB;

class TaskController extends Controller{
    /**
     * 展示通讯录
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function setting(Task $task){

        return view('admin.test.task',compact('task'));
    }
    public function index()
    {
        return view('admin.task.index');
    }
    //批量删除
    public function del(){
        $sql="delete from tasks where";
        $column="";
        for($i=0;$i<count($_REQUEST);$i++)
        {
            if($i==(count($_REQUEST)-1)){
                $column=$column."id=".$_REQUEST[$i];
            }else{
                $column=$column."id=".$_REQUEST[$i]." "."or"." ";
            }
        }
        $sql=$sql." ".$column;
        DB::select($sql);
        return [
            "code" => 200,
            'msg' => '删除成功',
        ];
        // $this->DeleteAll( $_REQUEST); //另类删除手法
    }
    /**
     * 数据接口
     */

    public function data()
    {
        $count=Task::count();
        if ($count>0) {
            if(isset($_GET['key']) && $_GET['key']['key']!="") {
                $key= $_GET['key']['key'];//搜索序列号
                $value= $_GET['key']['value'];//搜索序列号
                if($key!="ID")
                {

                    $users = Task::where($key,'like','%'.$value.'%')->with('all_task')->get();
                    $n=0;
                    foreach( $users as $a1)
                    {
                        $users[$n]['type_name']=$a1['all_task']->name;
                        switch ($a1['task_state']){
                            case 0:$users[$n]['task_state']='失败';break;
                            case 1:$users[$n]['task_state']='正在进行';break;
                            case 2:$users[$n]['task_state']='完成';break;
                        }
                        $n=$n+1;
                    };

                }
                else{

                    $users = Task::where([$key=>$value])->with('all_task')->get();
                    $n=0;
                    foreach( $users as $a1)
                    {
                        $users[$n]['type_name']=$a1['all_task']->name;
                        switch ($a1['task_state']){
                            case 0:$users[$n]['task_state']='失败';break;
                            case 1:$users[$n]['task_state']='正在进行';break;
                            case 2:$users[$n]['task_state']='完成';break;
                        }
                        $n=$n+1;
                    };
                }
            }
            else{
                $page = $_GET['page']; //当前页数
                $limit = $_GET['limit']; //每页的数量
                $page_count = ($page - 1) * $limit;
                $users = Task::offset($page_count)->limit($limit)->with('all_task')->get();
                //$users[0]['user_id']=$users[0]['type']['name'];
                $n=0;
                foreach( $users as $a1)
                {
                    $users[$n]['type_name']=$a1['all_task']->name;
                    switch ($a1['task_state']){
                        case 0:$users[$n]['task_state']='失败';break;
                        case 1:$users[$n]['task_state']='正在进行';break;
                        case 2:$users[$n]['task_state']='完成';break;
                    }
                    $n=$n+1;
                };
            }
            return json_encode(
                [
                    "code" => 0,
                    'msg' => '',
                    'count' => $count,
                    'data' => $users
                ]
            );
        } else{
            return [
                'code'=>0,
                "msg"=>"",
                "count"=>0,
                "data"=>[]
            ];
        }
    }
    /**
     * 添加界面
     */
    public function create(){
        $users=AdminUser::get();
        $types=TaskType::get();
        return view('admin.Task.create',compact('users','types'));
    }
    function array_remove1($data,$delKey) {
        $newArray = array();
        if(is_array($data)) {
            foreach($data as $key => $value) {
                if($key !== $delKey) {
                    $newArray[$key] = $value;
                }
            }
        }else {
            $newArray = $data;
        }
        return $newArray;
    }
    public function task_create(){
        return view('admin.task.task_create');
    }
    public function task_store(){
        $param=$_REQUEST;
        TaskType::create($param);
        return "<script> window.parent.location.reload();</script>";
    }
    /**
     * 添加操作逻辑简单实现
     */
    public function store(){
        $param=$_POST;
        $start=explode('到',$_REQUEST['datetime'])[0];
        $end=explode('到',$_REQUEST['datetime'])[1];
        $start=rtrim($start);
        $end=ltrim($end);
        $param['start_time']=$start;
        $param['end_time']=$end;
        $param = $this->array_remove1($param, 'datetime');
        Task::create($param);
        return redirect("/admin/task");
    }
    /*
     * 删除
     */
    public function delete($id)
    {
        Task::where('id', '=', $id)->delete();
        return back();
    }
    //修改界面
    public function edit(Task $task){
        $users=AdminUser::get();
        $types=TaskType::get();

        foreach($types as $key=>$value){

            if($value->id==$task->type_id)
            {
                $task->type_name=$value->name;
            }
        }
        foreach($users as $key=>$value){

            if($value->id==$task->user_id)
            {
                $task->user_name=$value->name;
            }
        }
        switch ($task['task_state']){
            case 0:$task['task_state']='失败';break;
            case 1:$task['task_state']='正在进行';break;
            case 2:$task['task_state']='完成';break;
        }
        //$task->type_name=;

        return view('admin.Task.update',compact('users','types','task'));
    }
    public function view_update(Task $Task){
        $Task->name=request('name'); //创建字段
        $Task->user_id=request('user_id'); //创建字段
        $Task->name=request('name'); //创建字段
        $Task->type_id=request('type_id');
        $Task->task_content=request('task_content');
        $start=explode('到',$_REQUEST['datetime'])[0];
        $end=explode('到',$_REQUEST['datetime'])[1];
        $start=rtrim($start);
        $end=ltrim($end);
        $Task->start_time=$start;
        $Task->end_time=$end;
        $Task->task_state=request('task_state');
        $Task->save();

        return redirect("/admin/task");///
    }
    //修改逻辑
    public function update(){
      $filds=$this->array_hebing($_REQUEST,'name','user_id','task_content');//传入需要更新的字段
      if(empty($filds))  //判断数据是否为空 是则不修改
      {
          return ['statu'=>'0'];
      }
      else{
          Task::where('id', $_REQUEST['id'])->update($filds);
          return ['statu'=>'200'];
      }

    }
    //批量删除逻辑
    //原理则是前端发送批量删除的ID组成的数组 通过后端逻辑进行处理

    /**
     * //组成一个我所要的数组用于数据库更新 并且
     * @param array ...$numbers 传进去的字段
     * @return array /￥/返回一个键=字段 值=数值的数组
     */
    public function array_hebing($Data,...$numbers){
      //  print_r($Data);die;

        $data=Task::where('id', $Data['id'])->first();
        //print_r($_REQUEST);die;
        $filds=[];
        if($Data && is_array($Data)){   //组成一个我所要的数组用于数据库更新
            //print_r($_REQUEST);die;
            foreach($numbers as $fild){
                foreach ($Data as $key=> $value){
                    if($fild==$key)
                    {
                            $filds[$fild] = $value;
                            continue;
                    }
                }
            }
            $start=0;  //循环记数
            foreach($filds as $key=>$value)  //删除请求更新字段中没有修改的字段
            {
                if($data[$key]==$filds[$key]) {   //判断是否相同 相同则从需要更新的数组中删除该键值
                        array_splice($filds, $start, 1);
                        $start=$start-1;
                }
                $start=$start+1;
            }
        }
        return $filds;
    }
}